1399C - Boats Competition - CodeForces Solution


brute force greedy two pointers *1200

Please click on ads to support us..

Python Code:

from collections import Counter, defaultdict
for _ in range(int(input())):
    n = int(input()) 
    cnt = Counter([int(i) for i in input().split()])
    d = [0] * 110
    for i in cnt : 
        for j in cnt :  d[i+j] += min(cnt[i], cnt[j])
 
    print(max(d)//2) 

C++ Code:

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <deque>
#include <cstring>
using namespace std;
const int N=2e5+10,INF=0x3f3f3f3f,mod=1e9+7;
typedef pair<int,int>PII;
typedef long long LL;
int a[N];
int st[N];
void solve()
{ 
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    
    int ans=0;
    for(int s=1;s<=100;s++)
    {
      int res=0;
      for(int i=1;i<=n;i++)st[i]=0;
	  for(int i=1;i<n;i++)
	  {
	  	for(int j=i+1;j<=n;j++)
	  	{
	  	  if(a[i]+a[j]==s&&!st[i]&&!st[j])
		  {
		  	res++;
		  	st[i]=st[j]=true;
		  	break;
		  }
		}
	  }
	  ans=max(ans,res);
	}
	cout<<ans<<'\n';
} 
signed main()
{
   int T=1;
   cin>>T; 
   while(T--)solve();
}


Comments

Submit
0 Comments
More Questions

1225A - Forgetting Things
1717A - Madoka and Strange Thoughts
1717B - Madoka and Underground Competitions
61B - Hard Work
959B - Mahmoud and Ehab and the message
802G - Fake News (easy)
1717C - Madoka and Formal Statement
420A - Start Up
1031A - Golden Plate
1559C - Mocha and Hiking
427B - Prison Transfer
330A - Cakeminator
426A - Sereja and Mugs
363A - Soroban
1585C - Minimize Distance
1506E - Restoring the Permutation
1539A - Contest Start
363D - Renting Bikes
1198D - Rectangle Painting 1
1023B - Pair of Toys
1725A - Accumulation of Dominoes
1675E - Replace With the Previous Minimize
839A - Arya and Bran
16B - Burglar and Matches
1625B - Elementary Particles
1725G - Garage
1725B - Basketball Together
735A - Ostap and Grasshopper
1183B - Equalize Prices
1481A - Space Navigation